using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameFrameWork;

/// <summary>
/// 散射攻击策略 - 同时发射多颗子弹，形成扇形攻击
/// </summary>
public class SpreadStrategy : AttackStrategyBase
{
    public float Cooldown => AttackData?.Interval ?? 2.0f;
    public AttackType AttackType => AttackType.SpreadShot;

    public SpreadStrategy(SkillData attackData)
    {
        AttackData = attackData;
    }

    public override void Execute(Entity target)
    {
        base.Execute(target);

        if (target == null)
        {
            Logger.LogWarning("SpreadStrategy", $"{Owner?.name} 攻击目标无效");
            return;
        }

        if (AttackData == null)
        {
            Logger.LogError("SpreadStrategy", $"{Owner?.name} 缺少攻击数据");
            return;
        }

        SpreadData spread = AttackData as SpreadData;
        if (spread == null)
        {
            Logger.LogError("SpreadStrategy", $"{Owner?.name} 攻击数据不是SpreadData类型");
            return;
        }

        if (AttackData.BulletData == null)
        {
            Logger.LogError("SpreadStrategy", $"{Owner?.name} 缺少子弹数据");
            return;
        }

        // 计算散射角度
        float[] angles = CalculateSpreadAngles(spread.SpreadCount, spread.SpreadAngle);

        // 获取发射位置
        Vector3 startPos = Owner.transform.position;
        if (AttackData.BulletData.OffVec3 != Vector3.zero)
        {
            startPos += AttackData.BulletData.OffVec3;
        }

        // 计算朝向目标的方向
        Vector3 directionToTarget = (target.transform.position - startPos).normalized;
        float baseAngle = Mathf.Atan2(directionToTarget.y, directionToTarget.x) * Mathf.Rad2Deg;

        // 发射多颗子弹
        foreach (float angle in angles)
        {
            var bulletData = AttackData.BulletData;
            var bullet = BulletPool.Instance.GetBullet(bulletData);
            
            if (bullet == null)
            {
                Logger.LogError("SpreadStrategy", $"无法获取子弹：{bulletData.BulletType}");
                continue;
            }

            // 计算散射方向（相对于目标方向）
            float finalAngle = baseAngle + angle;
            Quaternion rotation = Quaternion.Euler(0, 0, finalAngle);

            bullet.transform.SetPositionAndRotation(startPos, rotation);
            
            var movement = MoveStrategyFactory.Create(bulletData.MoveData);
            bullet.Initialize(
                owner: Owner,
                movement: movement);
        }

        Logger.Log("SpreadStrategy", $"{Owner?.name} 发射 {spread.SpreadCount} 发散射弹攻击 {target.name}");
    }

    /// <summary>
    /// 计算散射角度数组
    /// </summary>
    private float[] CalculateSpreadAngles(int count, float totalAngle)
    {
        if (count <= 0)
        {
            return new float[0];
        }

        float[] angles = new float[count];
        
        if (count == 1)
        {
            angles[0] = 0;
        }
        else
        {
            float startAngle = -totalAngle / 2;
            float step = totalAngle / (count - 1);

            for (int i = 0; i < count; i++)
            {
                angles[i] = startAngle + step * i;
            }
        }
        
        return angles;
    }
}
